package cn.qihua.action;

import java.sql.Timestamp;

import org.apache.struts2.json.annotations.JSON;

import cn.qihua.model.BaseProduct;
import cn.qihua.service.IProductService;

import com.opensymphony.xwork2.ActionSupport;

/**
 * @author 米修
 * 
 */

public class ProductAction extends ActionSupport {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	public String message;
	private boolean success = false;
	private int id;
	private int oid;
	private int uid;
	private int year;
	private String siteUrl;
	private IProductService productService;
	private BaseProduct product = new BaseProduct();

	public String findById() {
		this.setProduct(productService.findById(id));
		if (product == null) {
			this.setSuccess(false);
			this.setMessage("加载失败！");
			return SUCCESS;
		}
		this.setSuccess(true);
		//this.setMessage("");
		return SUCCESS;
	}

	public String allotOrder() {
		product.setOrderId(oid);
		product.setUserId(uid);
		product.setSiteUrl(siteUrl);
		product.setYears(1);
		product.setIsDisuse(false);
		product.setProgress("正在建站");
		product.setAcceptDate(new Timestamp(System.currentTimeMillis()));
		product.setStatus(true);
		if (productService.add(product)) {
			this.setSuccess(true);
			this.setMessage(product.getSiteUrl() + " 分配成功！");
			return SUCCESS;
		}
		this.setSuccess(false);
		this.setMessage("添加失败！");
		return ERROR;
	}

	public String editPdt() {
		if (product != null) {
			productService.edit(product);
			this.setSuccess(true);
			this.setMessage("修改成功！");
			return SUCCESS;
		} else {
			this.setSuccess(false);
			this.setMessage("修改失败！");
			return ERROR;
		}
	}

	// 续费
	public String goon() {
		if (productService.changeYears(id, year)
				&& productService.changePropertyById(id, "isDisuse", false)) {
			this.setSuccess(true);
			this.setMessage("续费成功！");
			return SUCCESS;
		} else {
			this.setSuccess(false);
			this.setMessage("操作失败！");
			return ERROR;
		}
	}

	// 不续费
	public String stop() {
		Boolean b = true;
		if (productService.changePropertyById(id, "isDisuse", b)) {// FALSE
			this.setSuccess(true);
			this.setMessage("不续费,将不显示于通知列表！");
			return SUCCESS;
		} else {
			this.setSuccess(false);
			this.setMessage("操作失败！");
			return ERROR;
		}
	}

	// 建站完成
	public String finished() {
		this.setProduct(productService.findById(id));
		if (product != null) {
			product.setProgress("已建成");
			product.setFinishDate(new Timestamp(System.currentTimeMillis()));
			productService.update(product);
			this.setSuccess(true);
			this.setMessage(product.getText() + "：建站进度(已 完成)");
			return SUCCESS;
		} else {
			this.setSuccess(false);
			this.setMessage("操作失败！");
			return ERROR;
		}
	}

	public String changePdtSta() {
		if (productService.changeStatus(id, true)) {
			this.setSuccess(true);
			this.setMessage("修改成功！");
			return SUCCESS;
		} else {
			this.setSuccess(false);
			this.setMessage("修改失败！");
			return ERROR;
		}
	}

	public String delPdt() {
		if (productService.del(id)) {
			this.setSuccess(true);
			this.setMessage("删除成功！");
			return SUCCESS;
		} else {
			this.setSuccess(false);
			this.setMessage("删除失败！");
			return ERROR;
		}
	}

	public String search() {
		return SUCCESS;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public int getYear() {
		return year;
	}

	public void setYear(int year) {
		this.year = year;
	}

	public boolean isSuccess() {
		return success;
	}

	public void setSuccess(boolean success) {
		this.success = success;
	}

	public int getOid() {
		return oid;
	}

	public void setOid(int oid) {
		this.oid = oid;
	}

	public int getUid() {
		return uid;
	}

	public void setUid(int uid) {
		this.uid = uid;
	}

	public String getSiteUrl() {
		return siteUrl;
	}

	public void setSiteUrl(String siteUrl) {
		this.siteUrl = siteUrl;
	}

	@JSON(name = "data")
	public BaseProduct getProduct() {
		return product;
	}

	public void setProduct(BaseProduct product) {
		this.product = product;
	}

	public void setProductService(IProductService productService) {
		this.productService = productService;
	}

}
